﻿using UnityEngine;
using System.Collections;

public class GameManager : MonoBehaviour {

    private BoxCollider2D leftBox;
    private BoxCollider2D rightBox;
    private BoxCollider2D upBox;
    private BoxCollider2D downBox;
    private GameObject player1, player2;

    // Use this for initialization
    void Start () {
        upBox = gameObject.transform.Find("UpWall").gameObject.GetComponent<BoxCollider2D>();
        downBox = gameObject.transform.Find("DownWall").gameObject.GetComponent<BoxCollider2D>();
        leftBox = gameObject.transform.Find("LeftWall").gameObject.GetComponent<BoxCollider2D>();
        rightBox = gameObject.transform.Find("RightWall").gameObject.GetComponent<BoxCollider2D>();
        player1 = GameObject.Find("Player1");
        player2 = GameObject.Find("Player2");
        InitWall();
        InitPlayer();
    }

    // Update is called once per frame
    void Update () {
        
	}

    void InitWall()
    {
        Vector3 topRight = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height,0));
        upBox.transform.position = new Vector3(0,topRight.y+0.5f);
        upBox.size = new Vector2(topRight.x * 2, 1);

        downBox.transform.position = new Vector3(0, -topRight.y-0.5f);
        downBox.size = new Vector2(topRight.x * 2, 1);

        leftBox.transform.position = new Vector3(-topRight.x-0.5f, 0);
        leftBox.size = new Vector2(1,topRight.y*2);

        rightBox.transform.position = new Vector3(topRight.x + 0.5f, 0);
        rightBox.size = new Vector2(1, topRight.y * 2);
    }

    void InitPlayer()
    {
        Vector3 leftPlayerPosition = Camera.main.ScreenToWorldPoint(new Vector3(200, Screen.height / 2, 0));
        leftPlayerPosition.z = 0;
        player1.transform.position = leftPlayerPosition;
        Vector3 rightPlayerPosition = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width - 200, Screen.height / 2, 0));
        rightPlayerPosition.z = 0;
        player2.transform.position = rightPlayerPosition;
    }

    public void ResetGame()
    {
        GameObject.Find("Ball").SendMessage("ResetBall");
    }
}
